$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'member/member/list',
        datatype: "json",
        colModel: [
			{ label: '编号', name: 'studentCode', width: 80 },
			{ label: '姓名', name: 'name', width: 80},
			{ label: '性别', name: 'sex', width: 80, formatter: "select", editoptions:{value:"0:女;1:男"}  },
			{ label: '出生日期', name: 'birthday', width: 80 },
			{ label: '人员类型', name: 'type', width: 80 , formatter: "select", editoptions:{value:"1:一类人员;2:二类人员;3:三类人员"}},
			{ label: 'IC编码', name: 'icCode', width: 80}
		],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

	new AjaxUpload('#upload', {
		action: baseURL + "member/member/upload/1",
		name: 'file',
		autoSubmit:true,
		responseType:"json",
		onSubmit:function(file, extension){
			// 校验文件格式
			if (!(extension && /^(xlsx|txt)$/.test(extension.toLowerCase()))){
			    alert('只支持xls,xlsx格式的文档！');
			    return false;
			}
		},
		onComplete : function(file, r){
			if(r.code == 0){
				alert(r.msg);
				vm.reload();
			}else{
				alert(r.msg);
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            name: null,
			sex: null,
			studentCode: null,
			type: null,
        },
		showList: true,
		title: null,
		member: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增人员";
			vm.member = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改人员";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.member.id == null ? "member/member/save" : "member/member/update";
			vm.member.birthday = $('#birthdayId').val();
			vm.member.icCode = $('#icCodeId').val();
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.member),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "member/member/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "member/member/info/"+id, function(r){
                vm.member = r.member;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.name, 'studentCode': vm.q.studentCode, 'type': vm.q.type, 'sex':vm.q.sex},
                page:page
            }).trigger("reloadGrid");
		},
		downloadExTemp: function() {
			var url = baseURL + "member/member/downloadExTemp";
			window.location.href = url;
		},
		downloadTxt: function () {
			var url = baseURL + "member/member/download/txt";
			window.location.href = url;
		},
		downloadEx: function () {
			var url = baseURL + "member/member/download";
			var params = '';
			if (vm.q.name) {
                params = params + "&name=" + vm.q.name;
            }
            if (vm.q.studentCode) {
                params = params + "&studentCode=" + vm.q.studentCode;
            }
			if (vm.q.type) {
				params = params + "&type=" + vm.q.type;
			}
			if (vm.q.sex) {
				params = params + "&sex=" + vm.q.sex;
			}
            if (params.length > 1) {
				url  = url + "?" + params.substring(1, params.length);
			}
			// alert(url);
            window.location.href = url;
		}
	}
});

$('#birthdayId').datepicker({
	language: 'zh-CN',
	format: 'yyyy-mm-dd'
});

function openRfid() {
	var com = $('#comId').val();
	if (!com) {
		alert("请输入COM口！");
		return;
	} else {
		$.ajax({
			type: "POST",
			url: baseURL + "member/member/openRfid?com="+com,
			contentType: "application/json",
			success: function(r){
				alert(r.msg);
			}
		});
	}
}

function closeRfid() {
	$.ajax({
		type: "POST",
		url: baseURL + "member/member/closeRfid",
		contentType: "application/json",
		success: function(r){
			alert(r.msg);
		}
	});
}

function findEpc() {
	$.ajax({
		type: "POST",
		url: baseURL + "member/member/findEpc",
		contentType: "application/json",
		success: function(r){
			if (r.epc) {
				$('#icCodeId').val(r.epc);
				vm.member.icCode = r.epc;
			} else {
				alert('请打开读卡器并放上手环');
			}
		}
	});
}
